var commonUtil = require("../../../utils/commonUtil.js")
var globalData = getApp().globalData;
var httpsUrl = globalData.httpsUrl;
var imgUrl = globalData.imgUrl;
var gcoord = require("../../../utils/gcoord.js")
Page({

  data: {
    imgUrl: imgUrl,
    Height: 0,
    scale: 11,
    latitude: "",
    longitude: "",
    markers: [],
    controls: [{
      id: 1,
      iconPath: "../../../image/common/jianhao.png",
      position: {
        left: 280,
        top: 100 - 50,
        width: 30,
        height: 25
      },
      clickable: true
    },
    {
      id: 2,
      iconPath: "../../../image/common/jiahao.png",
      position: {
        left: 320,
        top: 100 - 50,
        width: 30,
        height: 25
      },
      clickable: true
    }
    ],
    circles: [],
  },
  loadData: function () {
    var that = this;
    var markers = [];
    let params = new Object();
    params.plate = wx.getStorageSync("aidKey");
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '请稍等...', })
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/carinfo/getLastPositionByPlate",
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("car：" + JSON.stringify(res));
        if (res.code == 0) {
          var info = res.data;
          var gc = gcoord.transform([info.lon, info.lat], gcoord.WGS84, gcoord.GCJ02);//转换格式
          console.log("转换后经纬度："+gc);
          that.setData({
            latitude: gc[1],
            longitude: gc[0],
            markers: [{
              id: "1",
              latitude: gc[1],
              longitude: gc[0],
              width: 40,
              height: 46,
              iconPath: "../../../image/common/zuobiao.png",
              title: "最后位置"
            }],
            circles: [{
              latitude: gc[1],
              longitude: gc[0],
              color: '#24A7FF',
              fillColor: '#7cb5ec88',
              radius: 20,
              strokeWidth: 1
            }]
          })
        }else{
          wx.showModal({ title: '提示', content: res.message,})
        }
      }
    })
  },
  //点击地图事件
  clickMap: function (e) {
    console.log(JSON.stringify(e));
  },
  //视野发生变化时触发
  regionchange(e) {
    console.log("regionchange===" + e.type)
  },
  //点击merkers
  markertap(e) {
    console.log(e.markerId)
    // wx.showActionSheet({
    //   itemList: ["A"],
    //   success: function (res) {
    //     console.log(res.tapIndex)
    //   },
    //   fail: function (res) {
    //     console.log(res.errMsg)
    //   }
    // })
  },
  //点击缩放按钮动态请求数据
  controltap(e) {
    var that = this;
    console.log("scale===" + this.data.scale)
    if (e.controlId === 1) {
      that.setData({
        scale: --this.data.scale
      })
    } else {
      that.setData({
        scale: ++this.data.scale
      })
    }
  },
  onLoad: function (options) {
    this.loadData();
  },
  //腾讯API，WGS84转换为GCJ02坐标
  zhuanhuan:function(){
    let params2 = new Object();
    params2.locations = info.lat + ',' + info.lon;
    params2.type = 1;//1:GPS坐标
    params2.key = "6GCBZ-5NGKU-YXBVS-4JPA4-HVYQK-IMF7V";
    wx.request({
      // header: { "content-Type": "application/x-www-form-urlencoded" },
      url: "https://apis.map.qq.com/ws/coord/v1/translate",
      dataType: "json",
      method: "GET",
      data: params2,
      success: function (res) {
        //{"data":{"status":0,"message":"转换成功","locations":[{"lng":121.411142,"lat":31.098095}]},
        console.log("转换：" + JSON.stringify(res));
        console.log(res.data.locations[0].lng);
      }
    })
  }
})